package com.ruoyi.system.controller;

import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.utils.DictUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.ShCourse;
import com.ruoyi.system.api.domain.SysDictData;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.utils.Base64Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.service.IShCourseService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 课程信息Controller
 * 
 * @author ruoyi
 * @date 2023-07-18
 */
@RestController
@RequestMapping("/course")
public class ShCourseController extends BaseController
{
    @Autowired
    private IShCourseService shCourseService;

    /**
     * 查询课程信息列表
     */
    @RequiresPermissions("system:course:list")
    @GetMapping("/list")
    public TableDataInfo list(ShCourse shCourse)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            if (Base64Util.hasYwType(user.getYwType(), "1")) {
                shCourse.setAppId(user.getAppIdm());
            }else if (Base64Util.hasYwType(user.getYwType(), "2")) {
                shCourse.setAppId(user.getAppIdf());
            }else if (Base64Util.hasYwType(user.getYwType(), "3")) {
                shCourse.setAppId(user.getAppId());
            }
        }
        startPage();
        List<ShCourse> list = shCourseService.selectShCourseList(shCourse);
        return getDataTable(list);
    }

    /**
     * 导出课程信息列表
     */
    @RequiresPermissions("system:course:export")
    @Log(title = "课程信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShCourse shCourse)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            if (Base64Util.hasYwType(user.getYwType(), "1")) {
                shCourse.setAppId(user.getAppIdm());
            }else if (Base64Util.hasYwType(user.getYwType(), "2")) {
                shCourse.setAppId(user.getAppIdf());
            }else if (Base64Util.hasYwType(user.getYwType(), "3")) {
                shCourse.setAppId(user.getAppId());
            }
        }
        List<ShCourse> list = shCourseService.selectShCourseList(shCourse);
        List<SysDictData> dictLList = DictUtils.getDictCache("course_label");
        HashMap<String, String> dictLMap = new HashMap<>();
        for (SysDictData data:dictLList) {
            dictLMap.put(data.getDictValue(),data.getDictLabel());
        }
        for (ShCourse obj:list) {
            StringBuffer labelName = new StringBuffer();
            if (null != obj.getCourseLabel() && !"".equals(obj.getCourseLabel())) {
                String[] labelIdAry = obj.getCourseLabel().split(",");
                for (String labelId : labelIdAry) {
                    labelName.append(dictLMap.get(labelId)).append(",");
                }
            }
            obj.setLabelName(labelName.toString());
        }
        ExcelUtil<ShCourse> util = new ExcelUtil<ShCourse>(ShCourse.class);
        util.exportExcel(response, list, "课程信息数据");
    }

    /**
     * 获取课程信息详细信息
     */
    @RequiresPermissions("system:course:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(shCourseService.selectShCourseById(id));
    }

    @GetMapping("/pic/{id}")
    public R<ShCourse> getCourse(@PathVariable("id") Long id)
    {
        return R.ok(shCourseService.selectShCourseById(id));
    }

    /**
     * 新增课程信息
     */
    @RequiresPermissions("system:course:add")
    @Log(title = "课程信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShCourse shCourse)
    {
        return toAjax(shCourseService.insertShCourse(shCourse));
    }

    /**
     * 修改课程信息
     */
    @RequiresPermissions("system:course:edit")
    @Log(title = "课程信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShCourse shCourse)
    {
        return toAjax(shCourseService.updateShCourse(shCourse));
    }

    @PutMapping("/pic")
    public R<Integer> editCoursePic(@RequestBody ShCourse shCourse)
    {
        return R.ok(shCourseService.updateShCourse(shCourse));
    }

    /**
     * 删除课程信息
     */
    @RequiresPermissions("system:course:remove")
    @Log(title = "课程信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(shCourseService.deleteShCourseByIds(ids));
    }
}
